//
//  TT_ConversationListController.m
//  BaseProject
//
//  Created by 王洋 on 2020/4/28.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TUIConversationListController.h"
#import "TT_ConversationDetailViewController.h"

#import "TT_ConversationListController.h"

@interface TT_ConversationListController ()<
TUIConversationListControllerDelegate
>
@end

@implementation TT_ConversationListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}

- (void)initView {
    
    self.view.backgroundColor = UIColorHex(0xf1f1f1);
    
    TUIKitConfig *config = [TUIKitConfig defaultConfig];
    // 修改默认头像
    config.defaultAvatarImage = [UIImage imageNamed:@"guide_usericon"];
    config.avatarType = TAvatarTypeRadiusCorner;
    
    TUIConversationListController *conv = [[TUIConversationListController alloc] init];
    conv.view.backgroundColor = UIColorHex(0xf1f1f1);
    conv.delegate = self;
    [self addChildViewController:conv];
    [self.view addSubview:conv.view];
    
    [conv.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.right.mas_equalTo(0);
    }];
    
    [conv.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.right.mas_equalTo(0);
    }];
    
    [self.view setNeedsLayout];

    //如果不加这一行代码，依然可以实现点击反馈，但反馈会有轻微延迟，体验不好。
    conv.tableView.delaysContentTouches = NO;
    
    self.title = @"消息列表";

    for (TUIConversationCellData *cellData in conv.viewModel.dataList) {
        
        //系统消息置顶
        
        //过滤群聊
        
        //对于没有昵称的用户刷新
        if ([cellData.title isEqualToString:cellData.userID]) {
            
            NSArray *array = [NSArray arrayWithObject:cellData.userID];
            [[TT_IMManager sharedInstance] TTIM_GetUserProfile:array forceUpdate:YES succ:^(NSArray<TIMUserProfile *> *profiles) {
                
                TIMUserProfile *profile = profiles.firstObject;
                cellData.title = profile.nickname;
                [conv.tableView reloadData];
                
            } fail:^(int code, NSString *msg) {
                
            }];
        }
        
//        if (cellData.subTitle isEqualToAttributedString:<#(nonnull NSAttributedString *)#>) {
//            <#statements#>
//        }
    }
    [conv.tableView reloadData];
    
}

/**
 *在消息列表内，点击了某一具体会话后的响应函数
 */
- (void)conversationListController:(TUIConversationListController *)conversationController didSelectConversation:(TUIConversationCell *)conversation
{
    TT_ConversationDetailViewController *chat = [[TT_ConversationDetailViewController alloc] init];
    chat.conversationData = conversation.convData;
    [self.navigationController pushViewController:chat animated:YES];
}


@end
